package kr.ac.hanyang.cs.park;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SelectOption extends ListActivity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    // TODO Auto-generated method stub
	    ArrayList<OptionInfo> optionList			= null;

	    switch(CastSpell.getTaskType(getIntent().getStringExtra("TYPE").toString())) {
	    	case LAUNCH_APP:
	    		PkgInfo pkgInfo						= new PkgInfo(this);
	    		optionList							= pkgInfo.getPacakageInfo();
	    		Log.d("INFO", "GETTING APP LIST");
	    		break;
	    	
	    	case CALL_ADDR:
	    	case SEND_SMS:
	    		AddrInfo addrInfo					= new AddrInfo(this);
	    		optionList							= addrInfo.getAddressInfo();
	    		Log.d("INFO", "GETTING ADDR LIST");
	    		break;

	    	default:
	    		Log.e("ERROR", "NOTHING IMPLEMENTED YET @ "+ this.getClass().getSimpleName());
	    }

	    OptionListAdapter optionListAdapter			= new OptionListAdapter(this, R.layout.optionitems, optionList);
	    setListAdapter(optionListAdapter);

	}


    private class OptionListAdapter extends ArrayAdapter<OptionInfo> {
    	private ArrayList<OptionInfo> items;
    	
    	public OptionListAdapter(Context context, int textViewResourceId, ArrayList<OptionInfo> items) {
    		super(context, textViewResourceId, items);
    		this.items								= items;
    	}


    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v									= convertView;

    		if (v == null) {
    			LayoutInflater vi					= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    			v									= vi.inflate(R.layout.optionitems, null);
            }


    		OptionInfo option						= items.get(position);

    		if (option != null) {
    			ImageView icon						= (ImageView)v.findViewById(R.id.option_imgIcon);
    			TextView name						= (TextView) v.findViewById(R.id.option_txtName);
    			TextView value						= (TextView) v.findViewById(R.id.option_txtValue);
    			Button btn							= (Button)   v.findViewById(R.id.option_btnSelect);

    			if (icon != null)
    				icon.setImageDrawable(option.getIcon());

    			if (name != null)
    				name.setText(option.getName());

    			if (value != null)
    				value.setText(option.getOption());

    			// 현재 item의 position을 버튼에 tagging한 후, 클릭 시 이벤트 리스터에 넘김
    			btn.setTag(position);
    			btn.setOnClickListener(itemClickListener);
            }

            return v;
    	}


    	private View.OnClickListener itemClickListener = new View.OnClickListener() {
    		public void onClick(View v) {
    			OptionInfo option							= items.get((Integer) v.getTag());
    			TaskType taskType							= CastSpell.getTaskType(getIntent().getStringExtra("TYPE").toString());
    			String taskNameSuffix						= CastSpell.getTaskSuffix(taskType);
    			String taskName								= option.getName() + taskNameSuffix;
    			String taskOption							= option.getOption();
    			Drawable taskIcon							= option.getIcon();
    			
    			TaskInfo task								= new TaskInfo(taskName, taskType, taskIcon, taskOption);

    			ParcelableTask taskToSend					= new ParcelableTask(task);

				Intent intent								= new Intent(SelectOption.this, RegisterTask.class);
				intent.putExtra("TASK", taskToSend);
				startActivity(intent);
    			
    			finish();
    		}
    		
    	};

    }
}
